local skel = fk.CreateSkill{
  name = "sj_zhoushuhuizhan__shihua",
}

Fk:loadTranslationTable{
  ["sj_zhoushuhuizhan__shihua"] = "十划",
  [":sj_zhoushuhuizhan__shihua"] = 
    "出牌阶段限一次，你可以展示一名角色两张牌，然后你可以选择：<br>"..
    "1.弃置两张点数不在两张展示牌之间的牌，然后分配其展示的一张牌；<br>"..
    "2.弃置一张点数介于两张展示牌之间的牌，视为对其使用伤害值为2的【杀】。<br>"..
    "背水：为本技能增添“转换技”或“限定技”标签。",
  ["#sj_zhoushuhuizhan__shihua"] = "十划：你可以展示一名角色两张牌，根据展示牌的点数弃牌，分配展示牌或视为对其使用【杀】",
  ["#sj_zhoushuhuizhan__shihua_show"] = "十划：展示 %dest 两张牌",
  ["#sj_zhoushuhuizhan__shihua_dis1"] = "十划：弃置两张点数不为 %arg 的牌，然后分配 %dest 展示的一张牌",
  ["#sj_zhoushuhuizhan__shihua_give"] = "十划：分配 %dest 展示的一张牌",

  ["#sj_zhoushuhuizhan__shihua_2"] = "弃置两张点数小于 %arg 或大于 %arg2 的牌",
  ["#sj_zhoushuhuizhan__shihua_3"] = "弃置一张点数小于 %arg2 且大于 %arg 的牌",
  ["#sj_zhoushuhuizhan__shihua_4"] = "背水：为〖十划〗增添“转换技”标签",
  ["#sj_zhoushuhuizhan__shihua_5"] = "背水：为〖十划〗增添“限定技”标签",

  ["#sj_zhoushuhuizhan__shihua_dis2"] = "十划：弃置两张点数小于 %arg 或大于 %arg2 的牌，然后分配 %dest 展示的一张牌",
  ["#sj_zhoushuhuizhan__shihua_dis3"] = "十划：弃置一张点数小于 %arg2 且大于 %arg 的牌，视为对 %dest 使用伤害为2的【杀】",
  ["#sj_zhoushuhuizhan__shihua_dis4"] = "十划（背水）1/2：弃置两张点数小于 %arg 或大于 %arg2 的牌，然后分配 %dest 展示的一张牌",
  ["#sj_zhoushuhuizhan__shihua_dis5"] = "十划（背水）2/2：弃置一张点数小于 %arg2 且大于 %arg 的牌，视为对 %dest 使用伤害为2的【杀】",
}

skel:addEffect("active", {
  prompt = "#sj_zhoushuhuizhan__shihua",
  target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
    return #to_select:getCardIds("he") > 1 and #selected == 0
  end,
  card_filter = Util.FalseFunc,
  target_num = 1,
  min_target_num = 1,
  can_use = function (self, player)
    return table.find(Fk:currentRoom().alive_players,function (t)
      return #t:getCardIds("he") > 1
    end) and player:usedSkillTimes(skel.name,Player.HistoryPhase) == 0
  end,
  on_use = function (self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local cards = room:askToChooseCards(player,{
      min = 2,
      max = 2,
      target = target,
      flag = "he",
      skill_name = skel.name,
      prompt = "#sj_zhoushuhuizhan__shihua_show::"..target.id,
    })
    room:showCards(cards)
    local numbers = table.map(cards,function (id)
      return Fk:getCardById(id).number
    end)
    table.sort(numbers)
    local dis_2 = function ()
      local tos, ids = room:askToChooseCardsAndPlayers(player, {
        min_num = 1,
        max_num = 1,
        min_card_num = 1,
        max_card_num = 1,
        targets = room.alive_players,
        pattern = tostring(Exppattern{ id = cards }),
        skill_name = skel.name,
        prompt = "#sj_zhoushuhuizhan__shihua_give::"..target.id,
        cancelable = false,
        expand_pile = cards,
      })
      if #tos > 0 and #ids > 0 then
        room:moveCardTo(ids, Card.PlayerHand, tos[1], fk.ReasonGive, skel.name, nil, true, player)
      end
    end
    local dis_1 = function ()
      local card = Fk:cloneCard("slash")
      if not player.dead and not target.dead and player:canUseTo(card,target,{bypass_distances = true, bypass_times = true}) then
        local use = { ---@type UseCardDataSpec
          from = player,
          tos = {target},
          card = card,
          extraUse = true,
          additionalDamage = 1,
        }
        room:useCard(use)
      end
    end
    local min,max = numbers[1],numbers[2]
    if min == max then
      if #room:askToDiscard(player,{
        min_num = 2,
        max_num = 2,
        skill_name = skel.name,
        include_equip = true,
        pattern = ".|^"..tostring(min).."|.|.|.|.|.",
        prompt = "#sj_zhoushuhuizhan__shihua_dis1::"..target.id..":"..min,
        cancelable = true,
      }) > 1 then
        dis_2()
      end
    else 
      local list = {
        "#sj_zhoushuhuizhan__shihua_2::"..target.id..":"..min..":"..max,
        "#sj_zhoushuhuizhan__shihua_3::"..target.id..":"..min..":"..max,
        "#sj_zhoushuhuizhan__shihua_4",
        "#sj_zhoushuhuizhan__shihua_5",
      }
      local s, ret = room:askToUseActiveSkill(player,{
        skill_name = "#sj_zhoushuhuizhan__shihua_choose",
        prompt = "#sj_zhoushuhuizhan__shihua_dis2::"..target.id..":"..min..":"..max,
        extra_data = {
          skillName = skel.name,
          extra_data = {
            min = min,
            max = max,
            id = target.id,
            list = list,--实际可选择项
        }},
      })
      if s and ret and ret.cards and ret.interaction then
        room:throwCard(ret.cards,skel.name,player,player)
        local index = table.indexOf(list,ret.interaction)
        local beishui = index > 2 -- 背水且弃牌
        if index == 3 then
          room:handleAddLoseSkills(player,"-sj_zhoushuhuizhan__shihua|sj_zhoushuhuizhan__shihua_switch",nil,false,true)
        elseif index == 4 then
          room:handleAddLoseSkills(player,"-sj_zhoushuhuizhan__shihua|sj_zhoushuhuizhan__shihua_limited",nil,false,true)
        end
        if #ret.cards == 2 then
          dis_2()
          if beishui then
            local s, ret = room:askToUseActiveSkill(player,{
              skill_name = "#sj_zhoushuhuizhan__shihua_choose",
              prompt = "#sj_zhoushuhuizhan__shihua_dis5::"..target.id..":"..min..":"..max,
              extra_data = {
                skillName = skel.name,
                extra_data = {
                  min = min,
                  max = max,
                  id = target.id,
                  list = {list[2]},--实际可选择项
              }},
            })
            if s and ret and ret.cards and ret.interaction then
              room:throwCard(ret.cards,skel.name,player,player)
            end
            beishui = s
          end
        end
        if beishui or #ret.cards == 1 then
          dis_1()
        end
      end
    end
  end,
})

skel:addEffect(fk.EventAcquireSkill, {  -- 保险起见
  can_refresh = function(self, event, target, player, data)
    local skills = {
      "sj_zhoushuhuizhan__shihua_switch",
      "sj_zhoushuhuizhan__shihua_limited",
      "sj_zhoushuhuizhan__shihua_switch_limited",
    }
    return player:hasSkill(skel.name,true,true) and data.skill:getSkeleton() and table.contains(skills,data.skill:getSkeleton().name)
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:handleAddLoseSkills(player, "-"..skel.name, nil, false, true)
  end,
})

return skel